#include <stdio.h>
#include <string.h>
#include "seqlist.h"

static void show_char(const void *data);
static void show_string(const void *data);
static int string_cmp(const void *data, const void *key);
static int char_cmp(const void *data, const void *key);

int main(int argc, char *argv[])
{
	// 存argv[1]字符串的每一个字符
	seqlist_t *c_list = NULL;
	// 存argv每一个字符串, 顺序表中存储argv每一个参数字符串的起始地址
	seqlist_t *str_list = NULL;
	int i;
	char *del;

	if (argc < 2)
		return 1;

	seqlist_init(&c_list, strlen(argv[1]), sizeof(char));
	seqlist_init(&str_list, argc,sizeof(char *));

	for (i = 0; argv[1][i]; i++)	
		seqlist_insert_sort(c_list, argv[1] + i, char_cmp);
	seqlist_traval(c_list, show_char);
	printf("\n");

	for (i = 0; i < argc; i++) {
		seqlist_insert(str_list, argv + i);
	}
	seqlist_traval(str_list, show_string);
	printf("\n");

	del = "hello";
	if (-1 == seqlist_delete(str_list, &del, string_cmp)) {
		printf("么有这个成员\n");
	} else {
		printf("删除成功\n");
		seqlist_traval(str_list, show_string);
		printf("\n");
	}


	seqlist_destroy(&c_list);
	seqlist_destroy(&str_list);
	// 不出错 str_list = NULL;
	seqlist_destroy(&str_list);

	return 0;
}

static void show_char(const void *data)
{
	char *d = (char *)data;
	printf("%c", *d);
}

static void show_string(const void *data)
{
	char **d = (char **)data;

	printf("%s ", *d);
}

static int string_cmp(const void *data, const void *key)
{
	char **d = (char **)data;
	char **k = (char **)key;

	return strcmp(*d, *k);
}

static int char_cmp(const void *data, const void *key)
{
	char *d = (char *)data;
	char *k = (char *)key;
	return *k - *d;
}



